﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using G15_Agent.Models.Transport;
using System.Net;

namespace G15_Agent.BUS
{
    public class BUS_Transport
    {
        //public Service.Transport_G07.WebServicesSoapClient serviceG07 { get; set; }
        public Service.Transport_G07.WebServices serviceG07 { get; set; }

        public BUS_Transport()
        {
            //serviceG07 = new Service.Transport_G07.WebServicesSoapClient("WebServicesSoap");
            serviceG07 = new Service.Transport_G07.WebServices();
        }

        #region WSTransport0 - Đăng nhập

        public bool DangNhap_G07(string userName, string password)
        {
            try
            {
                CookieContainer cookieJar = new CookieContainer();
                serviceG07.CookieContainer = cookieJar;
                return serviceG07.DangNhap(userName, password);
            }
            catch (System.Exception ex)
            {
                string logError = ex.ToString();
            }
            return false;
        }

        #endregion

        #region WSTransport1- Lâý danh sách các loại xe

        public List<LoaiXe> LayDanhSachLoaiXe_G07()
        {
            List<LoaiXe> result = new List<LoaiXe>();
            try
            {
                Service.Transport_G07.loaixe[] dsLoaiXe = serviceG07.WSLayDSLoaiXe();
                foreach (Service.Transport_G07.loaixe itemServer in dsLoaiXe)
                {
                    LoaiXe itemClient = new LoaiXe();

                    itemClient.chiPhiTaiXe = itemServer.ChiPhiTaiXe;
                    itemClient.dichVu = itemServer.DichVu;
                    itemClient.gia = itemServer.Gia;
                    itemClient.giaNgoaiGio = itemServer.GiaNgoaiGio;
                    itemClient.hieuXe = itemServer.HieuXe;
                    itemClient.id = itemServer.ID;
                    itemClient.mien = itemServer.Mien;
                    itemClient.phiLuuDem = itemServer.PhiLuuDem;
                    itemClient.soCho = itemServer.SoCho;
                    itemClient.tenXe = itemServer.TenXe;
                    itemClient.url = itemServer.Url;

                    result.Add(itemClient);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }


            if (result.Count == 0) return null;
            return result;
        }
        #endregion

        #region Tìm xe theo dịch vụ

        public List<ChiTietXe> TimXeTheoDichVu_G07(string dichvu)
        {
            List<ChiTietXe> result = new List<ChiTietXe>();
            try
            {
                Service.Transport_G07.ChiTietXe[] dsLoaiXe = serviceG07.TimXeDichVu(dichvu);
                foreach (Service.Transport_G07.ChiTietXe itemServer in dsLoaiXe)
                {
                    ChiTietXe itemClient = new ChiTietXe();
                    itemClient.hieuXe = itemServer.HieuXe;
                    itemClient.mien = itemServer.Mien;
                    itemClient.tenXe = itemServer.TenXe;

                    result.Add(itemClient);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }

            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region Tìm xe theo số chổ

        public List<ChiTietXe> TimXeTheoSoCho_G07(string socho)
        {
            List<ChiTietXe> result = new List<ChiTietXe>();

            try
            {
                Service.Transport_G07.ChiTietXe[] dsLoaiXe = serviceG07.TimXeSoCho(socho);
                foreach (Service.Transport_G07.ChiTietXe itemServer in dsLoaiXe)
                {
                    ChiTietXe itemClient = new ChiTietXe();
                    itemClient.hieuXe = itemServer.HieuXe;
                    itemClient.mien = itemServer.Mien;
                    itemClient.tenXe = itemServer.TenXe;

                    result.Add(itemClient);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }

            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region Bảng giá xe du lịch theo miền

        public List<BangGiaXeDuLich> LayBangGiaXeDuLichTheoMien_G07(string mien)
        {
            List<BangGiaXeDuLich> result = new List<BangGiaXeDuLich>();

            try
            {
                Service.Transport_G07.BangGiaXeDuLich[] dsBangGia = serviceG07.GiaTheoMien(mien);
                foreach (Service.Transport_G07.BangGiaXeDuLich itemServer in dsBangGia)
                {
                    BangGiaXeDuLich itemClient = new BangGiaXeDuLich();
                    itemClient.chiPhiVuotKm = double.Parse(itemServer.ChiPhiVuotKm);
                    itemClient.giaTien = int.Parse(itemServer.GiaTien);
                    itemClient.hieuXe = itemServer.HieuXe;
                    itemClient.maLoaiXe = itemServer.LoaiXe;
                    itemClient.loTrinh = itemServer.LoTrinh;
                    itemClient.soKm = int.Parse(itemServer.SoKm);
                    itemClient.soNgay = int.Parse(itemServer.SoNgay);

                    result.Add(itemClient);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }


            return result;
        }

        #endregion

        #region Bảng giá theo dịch vụ trong ngày

        public List<BangGiaTheoGio> BangGiaTheoDichVu_G07(string dichvu)
        {
            List<BangGiaTheoGio> result = new List<BangGiaTheoGio>();
            try
            {
                Service.Transport_G07.BangGiaTheoGio[] dsBangGia = serviceG07.BangGiaTrongNgay(dichvu);
                foreach (Service.Transport_G07.BangGiaTheoGio itemServer in dsBangGia)
                {
                    BangGiaTheoGio itemClient = new BangGiaTheoGio();
                    itemClient.loaiDichVu = dichvu;
                    itemClient.chiPhiThueTaiXe = double.Parse(itemServer.ChiPhiTaiXe);
                    itemClient.giaNgoaiGio = double.Parse(itemServer.GiaNgoaiGio);
                    itemClient.giaTien = double.Parse(itemServer.GiaTien);
                    itemClient.giaTrangTri = double.Parse(itemServer.GiaTrangTri);
                    itemClient.hieuXe = itemServer.HieuXe;
                    itemClient.maLoaiXe = itemServer.LoaiXe;
                    itemClient.mien = itemServer.Mien;
                    itemClient.phiLuuDem = double.Parse(itemServer.PhiLuuDem);

                    result.Add(itemClient);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }
            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region Lây danh sách lộ trình

        public List<LoTrinh> LayDanhSachLoTrinh_G07()
        {
            List<LoTrinh> result = new List<LoTrinh>();
            try
            {
                Service.Transport_G07.lotrinh[] dsLoTrinh = serviceG07.WSLayDSLoTrinh();
                foreach (Service.Transport_G07.lotrinh itemServer in dsLoTrinh)
                {
                    LoTrinh itemClient = new LoTrinh();

                    itemClient.nhungdiemdiqua = itemServer.nhungdiemdiqua;
                    itemClient.sokm = itemServer.sokm;
                    itemClient.songay = itemServer.songay;
                    itemClient.tenlotrinh = itemServer.tenlotrinh;

                    result.Add(itemClient);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }


            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region Lâý danh sách hiệu xe

        public List<string> LayDanhSachHieuXe_G07()
        {
            List<string> result = new List<string>();

            try
            {
                string[] dsLoaiXe = serviceG07.WSLayDSHieuXe();
                for (int i = 0; i < dsLoaiXe.Count(); i++)
                    result.Add(dsLoaiXe[i]);
            }
            catch (System.Exception ex)
            {
                return null;
            }


            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region Lâý danh sách hiệu miền

        public List<string> LayDanhSachMien_G07()
        {
            List<string> result = new List<string>();

            try
            {
                string[] dsMien = serviceG07.WSLayDSMien();
                for (int i = 0; i < dsMien.Count(); i++)
                    result.Add(dsMien[i]);
            }
            catch (System.Exception ex)
            {
                return null;
            }


            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region Lâý danh sách dịch vụ

        public List<string> LayDanhSachDichVu_G07()
        {
            List<string> result = new List<string>();

            try
            {
                string[] dsDichVu = serviceG07.WSLayDSDichVu();
                for (int i = 0; i < dsDichVu.Count(); i++)
                    result.Add(dsDichVu[i]);
            }
            catch (System.Exception ex)
            {
                return null;
            }


            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region Tìm xe theo tiêu chí
        // input hang là Hiệu xe
        public List<LoaiXe> TimXeTheoTieuChi_G07(string mien, string hang, string soCho, string dichVu)
        {
            List<LoaiXe> result = new List<LoaiXe>();

            try
            {
                Service.Transport_G07.loaixe[] dsLoaiXe = serviceG07.WSTimKiemXe(mien, hang, soCho, dichVu);
                foreach (Service.Transport_G07.loaixe itemServer in dsLoaiXe)
                {
                    LoaiXe itemClient = new LoaiXe();

                    itemClient.chiPhiTaiXe = itemServer.ChiPhiTaiXe;
                    itemClient.dichVu = itemServer.DichVu;
                    itemClient.gia = itemServer.Gia;
                    itemClient.giaNgoaiGio = itemServer.GiaNgoaiGio;
                    itemClient.hieuXe = itemServer.HieuXe;
                    itemClient.id = itemServer.ID;
                    itemClient.mien = itemServer.Mien;
                    itemClient.phiLuuDem = itemServer.PhiLuuDem;
                    itemClient.soCho = itemServer.SoCho;
                    itemClient.tenXe = itemServer.TenXe;
                    itemClient.url = itemServer.Url;

                    result.Add(itemClient);
                }
            }
            catch (System.Exception ex)
            {
                return null;
            }

            if (result.Count == 0) return null;
            return result;
        }

        #endregion

        #region Đặt xe

        public int DatXe_G07(string tenXe, string dichVu,
            DateTime ngayDi, DateTime ngayVe, int taiXe, int soLuong)
        {
            try
            {
                return serviceG07.WSDatXe(tenXe, dichVu, ngayDi, ngayVe, taiXe, soLuong);
            }
            catch (System.Exception ex)
            {
                return 0;
            }
        }

        #endregion

        #region Đặt xe theo tour

        public int DatXeTheoTour_G07(string loTrinh, string tenXe, 
            int soLuong, DateTime ngayDi, DateTime ngayVe, 
            int gioDon, string diaDiemDon, string ghiChu)
        {
            try
            {
                return serviceG07.WSDatXeTheoTour(loTrinh, tenXe, soLuong, ngayDi, ngayVe, gioDon, diaDiemDon, ghiChu);
            }
            catch (System.Exception ex)
            {
                return 0;
            }
        }

        #endregion

    }
}